﻿//将form转为AJAX提交by post
function ajaxSubmit(frm, fn,url) {
    var dataPara = getFormJson(frm);
    $.ajax(url, {
        data: JSON.stringify(dataPara),
        type: "POST", contentType: "application/json",
        success: fn
    });
}

//将form转为AJAX提交by modify
function ajaxModify(frm, fn, url) {
    var dataPara = getFormJson(frm);
    $.ajax(url, {
        data: JSON.stringify(dataPara),
        type: "PUT", contentType: "application/json",
        success: fn
    });
}

//将form中的值转换为键值对。
function getFormJson(frm) {
    var o = {};
    var a = $(frm).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value);
        } else {
            o[this.name] = this.value;
        }
    });
    return o;
}

//tab页
function gopage(n) {
    var tag = document.getElementById("menuCI").getElementsByTagName("li");
    var taglength = tag.length;
    for (i = 1; i <= taglength; i++) {
        document.getElementById("m" + i).className = "";
        document.getElementById("c" + i).style.display = 'none';
    }
    document.getElementById("m" + n).className = "on";
    document.getElementById("c" + n).style.display = '';
}


jQuery.extend(jQuery.validator.messages, {
    required: "*",
    remote: "请修正该字段",
    email: "请输入正确格式的电子邮件",
    url: "请输入合法的网址",
    date: "请输入合法的日期",
    dateISO: "请输入合法的日期 (ISO).",
    number: "请输入合法的数字",
    digits: "只能输入整数",
    creditcard: "请输入合法的信用卡号",
    equalTo: "请再次输入相同的值",
    accept: "请输入拥有合法后缀名的字符串",
    ip:"*",
    maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
    minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
    rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
    range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
    max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
    min: jQuery.validator.format("请输入一个最小为 {0} 的值"),
});


jQuery.validator.addMethod("ip", function (value, element) {
    var tel = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)($|(?!\.$)\.)){4}$/;
    return this.optional(element) || (tel.test(value));
}, "&nbsp;&nbsp;&nbsp;IP格式错误");

function CheckName(inputname,type) {
    var Table = $("#gridTable1").jqGrid('getRowData');
    for (var i = 0; i < Table.length; i++) {
        if (Table[i].cChr_DeviceName == $(inputname).val()) {
            //&& Table[i].cChr_Classification == type
            alert("该名称已存在");
            return false;
        }
    }
    return true;
}

$('#AlertMessage').dialog({
    autoOpen: false,
    width: 300,
    modal: true,
    buttons: {
        "取消": function () {
            $(this).dialog("close");
        }
    }
});

$('#ConfirmMessage').dialog({
    autoOpen: false,
    width: 300,
    modal: true,
    buttons: {
        "取消": function() {
            $(this).dialog('close');
            mDialogCallback(false);
        },
        "确定": function() {
            $(this).dialog('close');
            mDialogCallback(true);
        }
    }
});

var mDialogCallback;
function ShowMsg(msg, callback) {
    if (callback == null) {
        $('#AlertMessageBody').html(msg);
        $('#AlertMessage').dialog('open');
    }
    else {
        mDialogCallback = callback;
        $('#ConfirmMessageBody').html(msg);
        $('#ConfirmMessage').dialog('open');
    }
    $(".ui-dialog-buttonset").children().height("26px");
};